package bjx.com.kehu.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import bjx.com.kehu.R;
import bjx.com.kehu.contants.Constants;
import bjx.com.kehu.utils.MD5;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jqc on 2017/10/25.
 */

public class MyAccountActivity extends BaseActivity {

    ImageView mIVBack;
    TextView mTVTitle;

    LinearLayout mLLDeposit;
    LinearLayout mLLMoney;
    LinearLayout mLLTiXian;
    LinearLayout mLLInvoice;

    LinearLayout myaccount_ll_card;
    TextView mTVMoney;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_myaccount);
        mIVBack = getViewById(R.id.back);
        mTVTitle = getViewById(R.id.title);
        mTVTitle.setText("我的账户");

        mLLDeposit = getViewById(R.id.myaccount_ll_deposit);
        mLLMoney = getViewById(R.id.myaccount_ll_money);
        mLLTiXian = getViewById(R.id.myaccount_ll_tixian);
        mLLInvoice = getViewById(R.id.myaccount_ll_invoice);
        myaccount_ll_card = getViewById(R.id.myaccount_ll_card);


        mTVMoney = getViewById(R.id.myaccount_tv_money);
    }

    @Override
    protected void setListener() {
        mIVBack.setOnClickListener(this);
        mLLDeposit.setOnClickListener(this);
        mLLMoney.setOnClickListener(this);
        mLLTiXian.setOnClickListener(this);
        mLLInvoice.setOnClickListener(this);
        myaccount_ll_card.setOnClickListener(this);

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (userModel != null) {
            mEngine.getUserMoney(userModel.getUser_id(), MD5.getMessageDigest((userModel.getUser_id() + Constants.BASE_KEY).getBytes())).enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    try {
                        String str = response.body().string();
                        JSONObject jo = new JSONObject(str);
                        int status = jo.getInt("status");
                        String msg = jo.getString("msg");
                        if (status == 200) {
                            JSONObject jsonObject = jo.getJSONObject("result");
                            String money = jsonObject.getString("user_money");
                            String commission = jsonObject.getString("commission");
                            String frozenMoney = jsonObject.getString("frozen_money");
                            if (commission.length() > 0) {
                                mTVMoney.setText(commission + "元");
                            }
                        } else if (status == 201) {
                            showToast("用户不存在");
                        } else if (status == 100) {
                            showToast("请求失败");
                        } else {
                            showToast("请求异常");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.myaccount_ll_deposit: // 充值
                startActivity(new Intent(mApp, DepositActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
                break;
            case R.id.myaccount_ll_money: // 佣金
                break;
            case R.id.myaccount_ll_tixian: // 提现
                startActivity(new Intent(mApp, TiXianActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
                break;
            case R.id.myaccount_ll_invoice: // 申请发票
                startActivity(new Intent(mApp, WebViewActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).putExtra("url", Constants.BILL_URL).putExtra("title", "申请发票"));
                break;
            case R.id.back:
                finish();
                break;

            case R.id.myaccount_ll_card: // 卡片
                startActivity(new Intent(mApp, CardTicketVoucherListActivity.class).addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
                break;

            default:break;
        }
    }
}
